/**
 * 版权所有，侵权必究！
 */

package com.commons.tools.utils;


import com.commons.tools.exception.*;
import io.swagger.annotations.*;

import java.io.*;

/**
 * 响应数据
 *
 * @author
 * @since 1.0.0
 */
@ApiModel(value = "响应")
public class Result<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    private static final int CODE_SUCCESS = 200;
    /**
     * 编码：200表示成功，其他值表示失败
     */
    @ApiModelProperty(value = "编码：200表示成功，其他值表示失败")
    private int code = 200;
    /**
     * 消息内容
     */
    @ApiModelProperty(value = "消息内容")
    private String msg = "success";
    /**
     * 响应数据
     */
    @ApiModelProperty(value = "响应数据")
    private T data;

    public static <T> Result<T> ok(T data) {
        Result<T> result = new Result<>();
        result.setData(data);
        return result;
    }

    public boolean success(){
        return code == CODE_SUCCESS ? true : false;
    }

    public static Result error() {
        Result result = new Result();
        result.setCode(ErrorCode.INTERNAL_SERVER_ERROR);
        result.setMsg(MessageUtils.getMessage(result.getCode()));
        return result;
}

    public static Result error(int code) {
        Result result = new Result();
        result.setCode(code);
        result.setMsg(MessageUtils.getMessage(result.getCode()));
        return result;
    }

    public static Result error(int code, String msg) {
        Result result = new Result();
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }

    public static Result error(String msg) {
        Result result = new Result();
        result.setCode(ErrorCode.INTERNAL_SERVER_ERROR);
        result.setMsg(msg);
        return result;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
